﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Tasinma
{
    public partial class RafBazliCikis : XForm
    {
        private string RollIds { get; set; }
        public Rapor.KumasIrsaliye xrIrsaliye { get; set; }

        public RafBazliCikis()
        {
            InitializeComponent();
        }

        private void DepoTasinma_Load(object sender, EventArgs e)
        {
            gvTop.Init("GetPaletRollsForDepoTransfer", string.Empty);

            txtIrsaliyeNo.EditValue = gvTop.Dataset().Tables[1].Rows[0][0];

            gvTop.FormatAsNumber(1, "SistemMetraj", "FizikselMetraj");
            gvTop.AddSummary("SistemMetraj", "FizikselMetraj");

            gvTop.MakeReadOnly();
            gvTop.EnableRowDelete();

            bbiIrsaliye.ItemClick += bbiIrsaliye_ItemClick;
            bbiRaf.ItemClick += bbiRaf_ItemClick;
            bbiCikis.ItemClick += bbiCikis_ItemClick;

            gvTop.BestFitColumns();
        }

        void bbiCikis_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataTable dtTop = gvTop.DataTable();
            dtTop.AcceptChanges();

            int RollCount = dtTop.Rows.Count;

            if (XtraMessageBox.Show(RollCount + " adet topu İRSALİYESİZ çıkacaksınız. Emin misiniz ?", "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                return;

            IrsaliyeOlustur(false);
        }

        void bbiRaf_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (RafSec rs = new RafSec())
            {
                if (rs.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    gvTop.RefreshDataSource("GetPaletRollsForDepoTransfer", rs.ShelfIds);
                    gvTop.BestFitColumns();
                }
            }
        }

        void bbiIrsaliye_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvTop.DataTable().AcceptChanges();
            RollIds = gvTop.DataTable().GetValuesofSpecifiedColumn("RollId", ",");
            string IrsaliyeNo = txtIrsaliyeNo.EditValue.ToStr();

            if (RollIds == "")
            {
                ShowErrorMessage("Top barkodu okutun!");
                return;
            }

            if (IrsaliyeNo == "")
            {
                ShowErrorMessage("İrsaliye numarası girin!");
                return;
            }
            xrIrsaliye = new Rapor.KumasIrsaliye(IrsaliyeNo, RollIds, WhoAmI, "GetPaletKumasIrsaliyeByDepoTransfer");

            xrIrsaliye.PrintingSystem.EndPrint += PrintingSystem_EndPrint;
            xrIrsaliye.PrintingSystem.StartPrint += PrintingSystem_StartPrint;
            xrIrsaliye.PrinterName = "SAMSUNG ML 2550";

            xrIrsaliye.ShowPreview();
        }

        void PrintingSystem_StartPrint(object sender, DevExpress.XtraPrinting.PrintDocumentEventArgs e)
        {
            e.PrintDocument.PrinterSettings.Copies = 4;
            e.PrintDocument.PrinterSettings.Collate = false;
        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            IrsaliyeOlustur(true);
            xrIrsaliye.ClosePreview();
        }

        private void IrsaliyeOlustur(bool Irsaliye)
        {
            RollIds = gvTop.DataTable().GetValuesofSpecifiedColumn("RollId", ",");
            ExecuteNonQuery("createPaletKumasIrsaliyeByDepoTransfer", RollIds, txtIrsaliyeNo.EditValue, Irsaliye);

            gvTop.Dataset().Clear();
            gvTop.RefreshDataSource("GetPaletRollsForDepoTransfer", "");
            txtIrsaliyeNo.EditValue = gvTop.Dataset().Tables[1].Rows[0][0];
            gvTop.BestFitColumns();
        }


    }
}
